package com.webmeeting.chat.wordchat.dao.daomanagers;

import com.myframework.utils.exceptions.WMDaoException;
import com.myframework.utils.logutil.WMLogger;
import com.webmeeting.chat.wordchat.dao.daos.UserDAOMySQLImpl;
import com.webmeeting.chat.wordchat.dao.daos.interfaces.UserDAO;
import com.webmeeting.chat.wordchat.dao.entities.User;

public class UserManager {

	private static UserDAO	dao;

	/**
	 * Create a user with username and password specified
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public static long createUser(String username, String password, String nickname) {
		long result = 0;

		initDAO();
		try {
			result = dao.createUser(username, password, nickname);
		} catch (WMDaoException e) {
			WMLogger.error("", e);
		}

		return result;
	}

	/**
	 * Delete a user in database by specifying the username
	 * 
	 * @param username
	 */
	public static void deleteUser(String username) {
		initDAO();
		try {
			dao.deleteUser(username);
		} catch (WMDaoException e) {
			WMLogger.error("", e);
		}
	}

	public static User findUserByName(String username) {
		User result = null;

		initDAO();
		try {
			result = dao.findUserByName(username);
		} catch (WMDaoException e) {
			WMLogger.error("", e);
		}

		return result;
	}

	/**
	 * Initialize the DAO
	 * <p>
	 * Note:<br/>
	 * If dao does not exist, then create one, otherwise use the existing one
	 * </p>
	 */
	private static synchronized void initDAO() {
		if (null == dao) {
			dao = new UserDAOMySQLImpl();
		}
	}

}
